package com.myopen.config;

import java.io.File;

import com.alibaba.druid.filter.stat.StatFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;
import com.myopen.cachestatic.DicCacheStatic;
import com.myopen.directive.AuthDirective;
import com.myopen.interceptor.RequestInterceptor;
import com.myopen.interceptor.WeixinAuthInterceptor;
import com.myopen.kit.DateKit;
import com.myopen.model.Attr;
import com.myopen.model.Config;
import com.myopen.model.DicInfo;
import com.myopen.model.DicType;
import com.myopen.model.FileInfo;
import com.myopen.model.Log;
import com.myopen.model.Order;
import com.myopen.model.Product;
import com.myopen.model.ProductCategory;
import com.myopen.model.Resource;
import com.myopen.model.Role;
import com.myopen.model.Task;
import com.myopen.model.User;
import com.myopen.model.WebsiteColumn;
import com.myopen.model.WebsiteContact;
import com.myopen.model.WebsiteContent;
import com.myopen.route.AdminRoutes;
import com.myopen.route.CommonRoutes;
import com.myopen.route.FrontRoutes;

/**
 * 
 * @author wangsanfei
 *
 */
public class AppConfig extends JFinalConfig{

	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", true));

		String realUploadPath=JFinal.me().getServletContext().getRealPath("");
		if(!StrKit.isBlank(PropKit.get("file.storePath"))){
			me.setBaseUploadPath(PropKit.get("file.storePath"));
			me.setBaseDownloadPath(PropKit.get("file.storePath"));
		}else{
			realUploadPath=realUploadPath+ File.separator+me.getBaseUploadPath();
		}

		JFinal.me().getServletContext().setAttribute(ConstConfig.REAL_UPLOAD_PATH,realUploadPath);
	}

	@Override
	public void configEngine(Engine me) {

		me.setDevMode(true);

		//此处模板放在页面里单独引用，是为了方便大家理解
		// me.addSharedFunction("/admin/common/header.html");
		// me.addSharedFunction("/admin/common/footer.html");
		// me.addSharedFunction("/admin/common/left.html");
		// me.addSharedFunction("/admin/common/layout.html");
		
		me.addSharedFunction("/common/dic_get_select.html");
		me.addSharedFunction("/common/dic_get_value.html");
		
		me.addSharedObject("StrKit", new StrKit());
		me.addSharedObject("ConstConfig", new ConstConfig());
		me.addSharedObject("User",new User());
		me.addSharedObject("DateKit",new DateKit());
		me.addSharedObject("DicCacheStatic",new DicCacheStatic());
		me.addSharedObject("ctx_path", JFinal.me().getContextPath());

		me.addDirective("auth", AuthDirective.class);

		
		
	}

	@Override
	public void configHandler(Handlers me) {
		
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new RequestInterceptor());
		me.add(new SessionInViewInterceptor());
		me.add(new WeixinAuthInterceptor());
		
	}

	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
		dp.addFilter(new StatFilter()); // 添加 StatFilter 才会有统计数据 
		//dp.setDriverClass("com.mysql.cj.jdbc.Driver");
		dp.setTestWhileIdle(true);
	    dp.setTestOnBorrow(true);
		me.add(dp);
		
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp); 
		arp.addMapping("t_user", User.class);
		arp.addMapping("t_role", Role.class);
		arp.addMapping("t_resource", Resource.class);
		arp.addMapping("t_log", Log.class);
		arp.addMapping("t_dic_type", DicType.class);
		arp.addMapping("t_dic_info", DicInfo.class);
		arp.addMapping("t_task", Task.class);
		arp.addMapping("t_file_info", FileInfo.class);
		arp.addMapping("t_config", Config.class);

		arp.addMapping("t_website_column", WebsiteColumn.class);
		arp.addMapping("t_website_content", WebsiteContent.class);
		arp.addMapping("t_website_contact", WebsiteContact.class);

		arp.addMapping("t_shop_attr", Attr.class);
		arp.addMapping("t_shop_category", ProductCategory.class);
		arp.addMapping("t_shop_product", Product.class);
		arp.addMapping("t_shop_order", Order.class);
		

		me.add(arp);
		
		//Cron4jPlugin cp=new Cron4jPlugin();
		
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new CommonRoutes());
		me.add(new FrontRoutes()); // 前端路由
		me.add(new AdminRoutes()); // 后端路由
	}

	// 系统启动完成后回调
	public void onStart() {

	}

	// 系统关闭之前回调
	public void onStop() {
		Task.dao.initAllTask();//初始化任务，数据库配置的
	}

	/**
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 * 
	 * 使用本方法启动过第一次以后，会在开发工具的 debug、run config 中自动生成
	 * 一条启动配置，可对该自动生成的配置再添加额外的配置项，例如 VM argument 可配置为：
	 * -XX:PermSize=64M -XX:MaxPermSize=256M
	 */
	public static void main(String[] args) {
		/**
		 * 特别注意：Eclipse 之下建议的启动方式
		 */
		UndertowServer.create(AppConfig.class)
				.configWeb( builder -> {
					// 配置 Filter
					// builder.addFilter("myFilter", "com.abc.MyFilter");
					// builder.addFilterUrlMapping("myFilter", "/*");
					// builder.addFilterInitParam("myFilter", "key", "value");

					// // 配置 Servlet
					// builder.addServlet("myServlet", "com.abc.MyServlet");
					// builder.addServletMapping("myServlet", "*.do");
					// builder.addServletInitParam("myServlet", "key", "value");

					// // 配置 Listener
					// builder.addListener("com.abc.MyListener");

					// 配置 WebSocket，MyWebSocket 需使用 ServerEndpoint 注解
					//builder.addWebSocketEndpoint("com.myopen.controller.ServiceWebSocketController");
					//builder.addWebSocketEndpoint("com.myopen.controller.MessageWebSocketController");
				})
				.start();
		//JFinal.start("src/main/webapp", 9000, "/",5);
		//早前的eclipse如果启动错误，使用这种方式启动
		//JFinal.start("src/main/webapp", 8000, "/","5");

		/**
		 * 特别注意：IDEA 之下建议的启动方式，仅比 eclipse 之下少了最后一个参数
		 */
		// JFinal.start("src/main/webapp", 80, "/");
	}

}
